﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using Wicresoft.UnifyShow.Business;

namespace Wicresoft.UnifyShow2.Portal.Controllers
{
    [Authorize]
    public class AdminOSMaintainController : BaseController
    {
        private IUser_X user;
        private ISiteBranch siteBranch;
        private IOS os;

        protected override void Dispose(bool disposing)
        {
            ((IDisposable)user).Dispose();
            ((IDisposable)siteBranch).Dispose();
            ((IDisposable)os).Dispose();
        }

        [InjectionConstructor]
        public AdminOSMaintainController(IUser_X user, ISiteBranch siteBranch, IOS os)
        {
            this.user = user;
            this.siteBranch = siteBranch;
            this.os = os;
        }


        //
        // GET: /AdminOSMaintain/

        public ActionResult Index()
        {
            base.SetRoles(user);
            ViewBag.IsConfigAdmin = IsConfigAdmin;
            var USER_X = user.GetUser_XByName(User.Identity.Name);
            if (USER_X == null)
                throw new Exception("USER_X is null.");
            ViewBag.UserDept = USER_X.Department;
            ViewBag.UserCompany = USER_X.Company;

            if (!IsConfigAdmin) return new HttpStatusCodeResult(403);


            var listModel = os.GetAllOS();
            IList<SiteBranchModel> listSites = siteBranch.GetSiteALLBranch();
            Dictionary<decimal, string> idAndSiteNames = new Dictionary<decimal, string>();
            foreach (var item in listSites)
            {
                idAndSiteNames[item.ID] = item.Name;
            }
            ViewData["SiteIDAndName"] = idAndSiteNames;

            return View(listModel);
        }

        public ActionResult Tree()
        {
            base.SetRoles(user);
            ViewBag.IsConfigAdmin = IsConfigAdmin;
            var USER_X = user.GetUser_XByName(User.Identity.Name);
            if (USER_X == null)
                throw new Exception("USER_X is null.");
            ViewBag.UserDept = USER_X.Department;
            ViewBag.UserCompany = USER_X.Company;

            if (!IsConfigAdmin) return new HttpStatusCodeResult(403);

            var listModel = os.GetAllOS().OrderBy(o => o.OSName);
            return PartialView(listModel);
        }

    }
}
